//
//  WeatherRequest.swift
//  MyWeatherApp
//
//  Created by Shengbing Li (QXU7859) on 2021/2/27.
//  Copyright © 2021 shanghai. All rights reserved.
//

import Alamofire
import SwiftyJSON
import ObjectMapper

class WeatherRequest: APIRequest {
    typealias APIResourceType = WeatherResource
    
    var path: String
    var method: HTTPMethod
    var parameters: Parameters?
    
    init(woeid: String) {
        self.method = .get
        self.path = "/api/location/\(woeid)/"
        self.parameters = ["": ""]
    }
}

class WeatherResource: APIResource {
}


class WeatherModel: Mappable {
    var consolidated_weather: [ConsolidatedWeatherModel]?
    var time: String?
    var sun_rise: String?
    var sun_set: String?
    var timezone_name: String?
    var parent: [String: Any]?
    var sources: [SourcesModel]?
    var title: String?
    var location_type: String?
    var woeid: Int?
    var latt_long: String?
    var timezone: String?
    
    required init(map: Map) {
    }
    
    // Mappable
    func mapping(map: Map) {
        consolidated_weather <- map["consolidated_weather"]
        time <- map["time"]
        sun_rise <- map["sun_rise"]
        sun_set <- map["sun_set"]
        timezone_name <- map["timezone_name"]
        parent <- map["parent"]
        sources <- map["sources"]
        title <- map["title"]
        location_type <- map["location_type"]
        woeid <- map["woeid"]
        latt_long <- map["latt_long"]
        timezone <- map["timezone"]
    }
    
}

class ConsolidatedWeatherModel: Mappable {
    var id: Int?
    var weather_state_name: String?
    var weather_state_abbr: String?
    var wind_direction_compass: String?
    var created: String?
    var applicable_date: String?
    var min_temp: Double?
    var max_temp: Double?
    var the_temp: Double?
    var wind_speed: Double?
    var wind_direction: Double?
    var air_pressure: Int?
    var humidity: Int?
    var visibility: Double?
    var predictability: Int?
    
    required init(map: Map) {
        
    }
    
    // Mappable
    func mapping(map: Map) {
        id <- map["id"]
        weather_state_name <- map["weather_state_name"]
        weather_state_abbr <- map["weather_state_abbr"]
        wind_direction_compass <- map["wind_direction_compass"]
        created <- map["created"]
        applicable_date <- map["applicable_date"]
        min_temp <- map["min_temp"]
        max_temp <- map["max_temp"]
        the_temp <- map["the_temp"]
        wind_speed <- map["wind_speed"]
        wind_direction <- map["wind_direction"]
        air_pressure <- map["air_pressure"]
        humidity <- map["humidity"]
        visibility <- map["visibility"]
        predictability <- map["predictability"]
    }
}


class SourcesModel: Mappable {
    var title: String?
    var slug: String?
    var url: String?
    var crawl_rate: Int?
    
    required init?(map: Map) {
        
    }
    
    func mapping(map: Map) {
        title <- map["title"]
        slug <- map["slug"]
        url <- map["url"]
        crawl_rate <- map["crawl_rate"]
    }
    
    var debugDescription: String {
        return """
        SourcesModel log:
            title: \(title ?? ""),
            slug: \(slug ?? ""),
            url: \(url ?? ""),
            crawl_rate: \(crawl_rate ?? 0)
        """
    }
}
